<!doctype html>
<html lang="zh">
<head>
	<meta charset="UTF-8" />
	<title>管理链接</title>
	<link href="/static/lunch/css/bootstrap.min.css" rel="stylesheet">
	<link href="/static/lunch/css/bootstrap-datetimepicker.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="panel panel-primary">
	<div class="panel-heading">
		<h3 class="panel-title">链接汇总 <a href="/lunch/admin_logout/">退出管理员登录</a></h3>
	</div>
	<ul class="list-group">
		<li class="list-group-item"><a href="/">点菜 - 首页</a></li>
		<li class="list-group-item" id="instead_href">
			<input type="text" id="username">
			<button id="btn_instead">帮某人点菜</button>
		</li>
		<li class="list-group-item">
			<input type="text" id="charge_user" placeholder="姓名全拼">
			<input type="number" id="charge_amount" placeholder="金额">
			<button id="btn_charge">充值</button>
		</li>
		<li class="list-group-item"><a href="/admin/">菜品添加，人员余额调整</a></li>
		<li class="list-group-item"><a href="/lunch/tongji/">点菜统计</a></li>
		<li class="list-group-item"><a href="/lunch/menu/">每月菜式更新</a></li>
	</ul>
</div>

<div class="panel panel-primary">
	<div class="panel-heading">
		<h3 class="panel-title">余额表格 合计余额: <span id="sum_balance"></span></h3>
	</div>
	<table class="table table-hover">
		<thead>
			<tr>
				<th>人员</th>
				<th>余额</th>
			</tr>
		</thead>
		<tbody>
			{% for people in peoples %}
			<tr>
				<td>{% if people.name %}
					{{people.name}}
					{% else %}
					{{people.user}}
					{% endif %}
				</td>
				<td class="balance">{{people.balance}}</td>
			</tr>
			{% endfor %}
		</tbody>
	</table>
</div>
</div>

<script src="/static/lunch/js/jquery-1.9.1.min.js"></script>
<script src="/static/lunch/js/bootstrap.min.js"></script>
<!--[if lt IE 9]>
  <script src="/static/lunch/js/html5shiv.js"></script>
  <script src="/static/lunch/js/respond.min.js"></script>
<![endif]-->
<script>
Date.prototype.Format = function(fmt)   
{ //author: meizz   
  var o = {   
    "M+" : this.getUTCMonth()+1,
    "d+" : this.getUTCDate(),                    //日   
    "h+" : this.getUTCHours(),                   //小时   
    "m+" : this.getUTCMinutes(),                 //分   
    "s+" : this.getUTCSeconds(),                 //秒   
    "q+" : Math.floor((this.getUTCMonth()+3)/3), //季度   
    "S"  : this.getUTCMilliseconds()             //毫秒   
  };   
  if(/(y+)/.test(fmt))   
    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));   
  for(var k in o)   
    if(new RegExp("("+ k +")").test(fmt))   
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
  return fmt;   
}

$("#btn_instead").click(function() {
	var username = $("#username").val();
	if(username === "" || username === undefined) {
		alert("输入正确的姓名全拼");
		return "";
	}
	$.ajax({
		type: 'GET',
		url: '/lunch/instead/' + username + '/',
		success: function(href) {
			var el = $(href)[0];
			el.target = "_blank";
			$("#instead_href").append(el);
		},
		error: function(err, ehr, obj) {
			//
		}
	});
});

$("#btn_charge").click(function() {
	var user = $("#charge_user").val();
	var amount = $("#charge_amount").val();
	if(user === "" || user === undefined || amount === "" || amount === undefined) {
		alert("输入不正确，金额与用户姓名全拼必须正确输入");
		return "";
	}
	$.ajax({
		type:"POST",
		url:"charge/" + user + "/",
		data: {
			'amount': amount
		},
		success: function(obj) {
			alert(obj);
			window.location.href='';
		},
		error: function(ehr, err, obj) {
			alert(ehr.responseText);
		}
	});
});

$(document).ready(function() {
	$("#charge_user").val = "";
	$("#charge_amount").val = "";
	$("#username").val = "";

	var bal_els = $(".balance");
	var sum = 0.0;
	for(var i=0; i != bal_els.length; ++i) {
		var cur_el = bal_els[i];
		var bal = parseFloat(cur_el.innerHTML);
		sum += bal;
	}
	
	$("#sum_balance").html(sum);
});
</script>
</body>
</html>